package com.service.lx;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dao.lx.FunctionDAO;
import com.dao.lx.FunctionalclassifyDAO;
import com.entity.Dept;
import com.entity.Function;
import com.entity.Functionalclassify;
import com.entity.User;
import com.service.ServiceException;


@Service
@Transactional(rollbackFor = Exception.class)
public class FunctionService {
	@Autowired
	private FunctionDAO functionDAO;
	@Autowired
	private FunctionalclassifyDAO functionalclassifyDAO;
	/**
	 * 添加/修改/逻辑删除
	 * @param function
	 * @throws ServiceException
	 */
	public void addAndUpdateFunction(Function function) throws ServiceException{
		try {
			if(function.getFunctionalclassifyId()!=null){
				Functionalclassify functionalclassify=functionalclassifyDAO.findOne(function.getFunctionalclassifyId());
				if(functionalclassify!=null){
					function.setFunctionalclassify(functionalclassify);
				}else{
					function.setFunctionalclassify(null);
				}
			}
			Function function2=functionDAO.findOne(function.getFunctionId());
			if(function2==null){
				function.setFunctionIfdelete(new BigDecimal(1));
			}
			function.setFunctionIntodate(new Date());
			functionDAO.save(function);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 删除
	 * @param functionid
	 * @throws ServiceException
	 */
	public void deleteFunction(Long functionid) throws ServiceException{
		try {
			functionDAO.delete(functionid);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 逻辑删除
	 * @param functionId
	 */
	public void UpdateFunctionIfdelete(Long functionId) {
		Function function = functionDAO.findOne(functionId);
		function.setFunctionIfdelete(new BigDecimal(0));
		addAndUpdateFunction(function);
	}
	
	/**
	 * 批量删除
	 * @param userIds
	 */
	public void deletArrIfDelete(List<Long> functionIds) {
		if(functionIds != null) {
			for(Long userId: functionIds) {
				Function function = functionDAO.findOne(userId);
				function.setFunctionIfdelete(new BigDecimal(0));
				addAndUpdateFunction(function);
			}
		}
	}
	/**
	 * 单条查询
	 * @param functionid
	 * @return
	 */
	public Function findOne(Long functionid) {
		return functionDAO.findOne(functionid);
	}
	

	/**
	 * 查询所有
	 * @param curpage
	 * @param pagesize
	 * @return
	 */
	public Page<Function> findAllFunctions(Integer curpage, Integer pagesize){
		return functionDAO.findAllFunctionIfdelete(new PageRequest(curpage-1, pagesize));
	}

	/**
	 * 查询功能id
	 * @param field
	 * @param curpage
	 * @param pagesize
	 * @return
	 */
	public Page<Function> findByFunctionName(String field, Integer curpage, Integer pagesize){
		return functionDAO.findByFunctionName(field, new PageRequest(curpage-1, pagesize));
	}
	
	/**
	 * 根据分类查询
	 * @param curpage
	 * @param pagesize
	 * @return
	 */
	public Page<Function> findAllByFunctionalclassifyId(String field, Integer curpage, Integer pagesize){
		Functionalclassify functionalclassify=functionalclassifyDAO.findOne(Long.parseLong(field));
		return functionDAO.findByFunctionalclassify(functionalclassify,new PageRequest(curpage-1, pagesize));
	}
	/**
	 * 查询
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Function> findUsersBy(String field, Integer curpage, Integer pagesize){
		String str;
		if(field.startsWith("functionName")){
			str=field.replace("functionName", "");
			return findByFunctionName(str, curpage, pagesize);
		}else if(field.startsWith("functionalclassify")){
			str=field.replace("functionalclassify", "");
			return findAllByFunctionalclassifyId(str, curpage, pagesize);
		}else{
			return findAllFunctions(curpage, pagesize);
		}
	}

}
